package com.yys.gulimall.cart.vo;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author zq
 * @version 1.0
 * @date 2021/3/31 16:15
 */
public class Cart {
    private List<CartItem> items;
    private Integer countNum;
    private Integer countType;
    private BigDecimal totalAmount;
    // 优惠价格
    private BigDecimal reduce = new BigDecimal("0.00");

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int count = 0;
        if(ObjectUtils.isNotEmpty(this.items)) {
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    public Integer getCountType() {
        int count = 0;
        if(ObjectUtils.isNotEmpty(this.items)) {
            for (CartItem ignored : items) {
                count += 1;
            }
        }
        return count;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        if(ObjectUtils.isNotEmpty(this.items)) {
            for (CartItem item : items) {
                if(item.getCheck()) {
                    amount = amount.add(item.getTotalPrice());
                }
            }
        }
        // 减去优惠总价
        amount = amount.subtract(this.getReduce());

        return amount;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
